package com.coldwindblows.blog.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coldwindblows.blog.bo.CommentBo;
import com.coldwindblows.blog.constant.WebConst;
import com.coldwindblows.blog.dao.CommentMapper;
import com.coldwindblows.blog.exception.TipException;
import com.coldwindblows.blog.pojo.CommentDo;
import com.coldwindblows.blog.pojo.ContentDo;
import com.coldwindblows.blog.service.ICommentService;
import com.coldwindblows.blog.service.IContentService;
import com.coldwindblows.blog.utils.TaleUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ICommentServiceImpl implements ICommentService {
	
	@Autowired
	private CommentMapper commentDao;
	
	@Autowired
	private IContentService contentService;

	@Transactional
	@Override
	public String insertComment(CommentDo commentDo) {
		if(null == commentDo) {
			return "评论对象为空";
		}
		if(StringUtils.isBlank(commentDo.getAuthor())) {
			commentDo.setAuthor("热心网友");
		}
		if(StringUtils.isNotBlank(commentDo.getMail()) && !TaleUtils.isEmail(commentDo.getMail())) {
			return "请输入正确的邮箱地址";
		}
		if(StringUtils.isBlank(commentDo.getContent())) {
			return "评论字数在5-2000个字符";
		}
		if(null == commentDo.getCid()) {
			return "评论文章不能为空";
		}
		ContentDo content = contentService.getDetail(String.valueOf(commentDo.getCid()));
		if(null == content) {
			return "不存在的文章";
		}
		commentDo.setOwnerId(content.getAuthorId());
		commentDo.setStatus("not_audit");
		commentDo.setCreated(new Date());
		commentDao.insert(commentDo);
		
		ContentDo temp = new ContentDo();
		temp.setCid(content.getCid());
		temp.setCommentsNum(content.getCommentsNum() + 1);
		contentService.updateContent(temp);
		
		return WebConst.SUCCESS_RESULT;
 	}

	@Override
	public PageInfo<CommentBo> getComments(Integer cid, int page, int pageSize) {
		if(null == cid) {
			return null;
		}
		PageHelper.startPage(page, pageSize);
		CommentDo temp = new CommentDo();
		temp.setCid(cid);
		temp.setParent(0);
		temp.setStatus("approved");
		List<CommentDo> parents = commentDao.selectBySelective(temp);
		PageInfo<CommentDo> pagelist = new PageInfo<>(parents);
		PageInfo<CommentBo> returnBo = copyPageInfo(pagelist, CommentBo.class);
		if(parents.size() == 0) {
			return returnBo;
		}
		List<CommentBo> comments = new ArrayList<>(parents.size());
		parents.forEach(parent -> {
			comments.add(new CommentBo(parent));
		});
		
		returnBo.setList(comments);
		
		return returnBo;
	}

	@Override
	public PageInfo<CommentDo> getCommentsWithPage(CommentDo comment, int page, int pageSize) {
		PageHelper.startPage(page, pageSize);
		List<CommentDo> list = commentDao.selectBySelective(comment);
		
		return new PageInfo<>(list);
	}

	@Override
	public CommentDo getDetail(Integer coid) {
		if(null == coid) {
			return null;
		}

		return commentDao.selectByPrimaryKey(coid);
	}

	@Transactional
	@Override
	public boolean deleteComment(Integer coid, Integer cid) {
		if(null == coid) {
			throw new TipException("主键为空");
		}
		boolean result = commentDao.deleteByPrimaryKey(coid) > 0;
		ContentDo content = contentService.getDetail(String.valueOf(cid));
		if(null == content || content.getCommentsNum() <= 0) {
			return result;
		}
		
		ContentDo temp = new ContentDo();
		temp.setCid(cid);
		temp.setCommentsNum(content.getCommentsNum() - 1);
		contentService.updateContent(temp);
		
		return result;
	}

	@Transactional
	@Override
	public boolean updateComment(CommentDo comment) {
		if(null == comment || null == comment.getCoid()) {
			return false;
		}
		
		return commentDao.updateByPrimaryKey(comment) > 0;
	}
	
	/**
	 * copy原有分页信息，除数据
	 * @param origin
	 * @return
	 */
	private <E> PageInfo<E> copyPageInfo(PageInfo<?> origin, Class<E> e) {
		PageInfo<E> result = new PageInfo<>();
		result.setPageSize(origin.getPageSize());
		result.setPageNum(origin.getPageNum());
		result.setStartRow(origin.getStartRow());
		result.setEndRow(origin.getEndRow());
		result.setTotal(origin.getTotal());
		result.setPages(origin.getPages());
		result.setHasNextPage(origin.isHasNextPage());
		result.setHasPreviousPage(origin.isHasPreviousPage());
		result.setIsFirstPage(origin.isIsFirstPage());
		result.setIsLastPage(origin.isIsLastPage());
		result.setNavigateFirstPage(origin.getNavigateFirstPage());
		result.setNavigateLastPage(origin.getNavigateLastPage());
		result.setNavigatePages(origin.getNavigatePages());
		result.setNavigatepageNums(origin.getNavigatepageNums());
		result.setSize(origin.getSize());
		result.setPrePage(origin.getPrePage());
		result.setNextPage(origin.getNextPage());
		
		return result;
	}

}
